﻿/*----------------------------------------------------------------
//  <copyright file="UserOrganizationController.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-11-22 15:11</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Api.Identity.Areas.Admin.Controllers.Identity
{
    #region "用户组织机构管理"
    /// <summary>
    /// 用户组织机构管理
    /// </summary>
    [Description("用户组织机构管理")]
    [ModuleInfo(Code = "UserOrganization", Name = "用户组织机构管理", OrderNo = 5, Position = "Identity", PositionName = "认证模块")]
    public class UserOrganizationController : AdminControllerBase
    {
        #region "属性"
        #region "获取 身份认证模块业务契约"
        /// <summary>
        /// 获取 身份认证模块业务契约
        /// </summary>
        protected IIdentityContract IdentityContract => ServiceProvider.GetService<IIdentityContract>();
        #endregion

        #endregion

        #region "Action方法"
        #region "读取用户组织机构"
        /// <summary>
        /// 读取用户组织机构
        /// </summary>
        /// <param name="request">分页请求</param>
        /// <returns></returns>
        [HttpPost]
        [ModuleInfo]
        [Description("读取")]
        public virtual PageData<UserOrganizationOutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<UserOrganization, bool>> predicate = FilterService.GetExpression<UserOrganization>(request.FilterGroup);
            Func<UserOrganization, bool> updateFunc = FilterService.GetDataFilterExpression<UserOrganization>(null, DataAuthOperation.Update).Compile();
            Func<UserOrganization, bool> deleteFunc = FilterService.GetDataFilterExpression<UserOrganization>(null, DataAuthOperation.Delete).Compile();

            //var page = IdentityContract.UserOrganizations.ToPage(predicate, request.PageCondition, m => new
            //{
            //    D = m,
            //    m.User.UserName,
            //    OrganizationName = m.Organization.Name,
            //}).ToPageResult(data => data.Select(m => new UserOrganizationOutputDto(m.D)
            //{
            //    UserName = m.UserName,
            //    OrganizationName = m.OrganizationName,
            //    Updatable = updateFunc(m.D),
            //    Deletable = deleteFunc(m.D)
            //}).ToArray());
            var page = CacheService.ToPageCache(IdentityContract.UserOrganizations, predicate, request.PageCondition, m => new
            {
                D = m,
                m.User.UserName,
                OrganizationName = m.Organization.Name,
            }, function).ToPageResult(data => data.Select(m => new UserOrganizationOutputDto(m.D)
            {
                UserName = m.UserName,
                OrganizationName = m.OrganizationName,
                Updatable = updateFunc(m.D),
                Deletable = deleteFunc(m.D)
            }).ToArray());
            return page.ToPageData();
        }
        #endregion
        #region "更新用户组织机构"
        /// <summary>
        /// 更新用户组织机构
        /// </summary>
        /// <param name="dtos">用户组织机构信息</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        public virtual async Task<AjaxResult> Update(params UserOrganizationInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));

            OperationResult result = await IdentityContract.UpdateUserOrganizations(dtos);
            return result.ToAjaxResult(true);
        }
        #endregion
        #region "删除用户组织机构"
        /// <summary>
        /// 删除用户组织机构
        /// </summary>
        /// <param name="ids">要删除的用户组织机构编号</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        public virtual async Task<AjaxResult> Delete(params long[] ids)
        {
            Check.NotNull(ids, nameof(ids));

            OperationResult result = await IdentityContract.DeleteUserOrganizations(ids);
            return result.ToAjaxResult();
        }
        #endregion

        #endregion

    }
    #endregion

}
